package com.ficus.road.maintain.strategy.operationlog;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ficus.road.maintain.core.model.OperationLog;
import com.ficus.road.maintain.operationlog.model.CheckData;
import com.ficus.road.maintain.operationlog.model.PushZHRequest;
import com.ficus.road.maintain.operationlog.model.PushZHResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @author xiawen yang
 * @date 2021/12/24 上午11:14
 */
@Slf4j
public class PushZHStrategy implements OperationLogStrategy {
    @Override
    public Object processRequestParam(String jsonString) {
        JSONArray jsonArray = JSONArray.parseArray(jsonString);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if (ObjectUtils.isNotEmpty(jsonObject)) {
                JSONObject iddr = jsonObject.getJSONObject("iddr");
                if (ObjectUtils.isNotEmpty(iddr)) {
                    JSONObject info = iddr.getJSONObject("info");
                    if (ObjectUtils.isNotEmpty(info)) {
                        String carId = info.getString("devid");
                        Double longitude = info.getDouble("lgt");
                        Double latitude = info.getDouble("lat");
                        String url = info.getString("url");
                        String roadName = info.getString("roadName");
                        String hitId = info.getString("sid");
                        String picName = info.getString("picName");
                        PushZHRequest pushZHRequest = PushZHRequest.builder().carId(carId).longitude(longitude).latitude(latitude)
                                .url(url).roadName(roadName).hitId(hitId).picName(picName).build();
                        return pushZHRequest;
                    }
                }
            }
        }
        return jsonString;
    }

    @Override
    public Object processResponse(String returnString, OperationLog operationLog) {
        // 推送中海的返回值已经是json字符串了，如果再次调用JsonObject.toJsonString，就不是json格式的(外面多一层引号)
        // 所以这里要将其反解析
        Object parse = JSON.parse(returnString);
        String s = parse.toString();
        PushZHResponse pushZHResponse = JSONObject.parseObject(s, PushZHResponse.class);
        if (ObjectUtils.isNotEmpty(pushZHResponse)) {
            log.info("推送中海的返回值为: {}", pushZHResponse);
            CheckData checkData = pushZHResponse.getUploadResult().getCheckData();
            if (ObjectUtils.isNotEmpty(checkData)) {
                String result = checkData.getResult();
                if (!result.equals("0")) {
                    operationLog.setType("error");
                    operationLog.setErrorMsg(checkData.getMessage());
                }
            }
            log.info("推送中海的返回值为: {}", pushZHResponse);
            return pushZHResponse;
        }
        log.info("推送中海的返回值为: {}", returnString);
        return returnString;
    }
}
